package com.cskaoyan.service.wx;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cskaoyan.Utils.Md5Utils;
import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.bean.userbean.User;
import com.cskaoyan.bean.userbean.UserExample;
import com.cskaoyan.bo.wx.WxRegisterBo;
import com.cskaoyan.config.AliyunCompoent;
import com.cskaoyan.config.Oss;
import com.cskaoyan.mapper.usermapper.UserMapper;
import com.cskaoyan.realm.MallToken;
import com.cskaoyan.service.userservice.UserService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.wx.WxLoginVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author hsp
 * @date 2021/5/12 16:04
 */
@Service
public class WxAuthServiceImpl implements WxAuthService {
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AliyunCompoent aliyunCompoent;
    @Override
    public WxLoginVo login(Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        Subject subject = SecurityUtils.getSubject();

        String avatarUrl = userService.selectAvatarByUsername(username);
        String md5SaltPassword = Md5Utils.getMd5Salt(password);
        MallToken wxMallToken = new MallToken(username, md5SaltPassword, "wx");
//        MallToken wxMallToken = new MallToken(username, password, "wx");
        subject.login(wxMallToken);
        Session session = subject.getSession();
        Serializable token = session.getId();
        Integer userId = userService.getUserIdByUsername(username);
        session.setAttribute("userId",userId);
        session.setAttribute("username",username);
        WxLoginVo wxLoginVo = new WxLoginVo(
                new WxLoginVo.UserInfoBean(username, avatarUrl
                        ),
                "tokenExpireIsWhat???",
                token.toString()

        );
        return wxLoginVo;
    }

    @Override
    public void sendMsg(String mobilePhone, String code) {


        aliyunCompoent.sendMsg(mobilePhone,code);


    }

    @Override
    public WxLoginVo register(WxRegisterBo wxRegisterBo, HttpServletRequest request) {

        User user = new User();
        user.setId(null);
        String username = wxRegisterBo.getUsername();
        String password = wxRegisterBo.getPassword();

        user.setUsername(username);
        String md5Password = Md5Utils.getMd5Salt(password);
//        user.setPassword(password);
        user.setPassword(md5Password);
        user.setNickname(username);

        user.setMobile(wxRegisterBo.getMobile());
        user.setWeixinOpenid(wxRegisterBo.getWxCode());
        user.setGender(new Integer(1).byteValue());
        user.setLastLoginIp(request.getRemoteAddr());
        user.setStatus(Byte.valueOf("0"));
        user.setBirthday(new Date()); //注册的日期
        user.setUserLevel(Byte.valueOf("0")); //普通用户
        user.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80");
        userMapper.insert(user);

        WxLoginVo wxLoginVo = new WxLoginVo();
        WxLoginVo.UserInfoBean userInfoBean = new WxLoginVo.UserInfoBean();
        userInfoBean.setNickName(username);
        userInfoBean.setAvatarUrl("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80");
        wxLoginVo.setToken(MyUtils.getSession().getId().toString());
        wxLoginVo.setTokenExpire("TokenExpireIsWhat??");
        wxLoginVo.setUserInfo(userInfoBean);
        Subject subject = SecurityUtils.getSubject();
        String md5SaltPassword = Md5Utils.getMd5Salt(password);
        MallToken wxMallToken = new MallToken(username, md5SaltPassword, "wx");
//        MallToken wxMallToken = new MallToken(username, password, "wx");
        subject.login(wxMallToken);
        Session session = subject.getSession();
        Integer userId = user.getId();
        session.setAttribute("userId",userId);
        session.setAttribute("username",username);




        return wxLoginVo;
    }

    @Override
    public void resetPassword(String mobile, String password) {

        UserExample userExample = new UserExample();
        userExample.createCriteria().andMobileEqualTo(mobile);
        User user = new User();
        user.setPassword(Md5Utils.getMd5Salt(password));
        userMapper.updateByExampleSelective(user,userExample);
    }

    @Override
    public boolean hasContainsUsername(String username) {

        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        Long userNum = userMapper.countByExample(userExample);
        int userNum1 = userNum.intValue();
        return  userNum1 != 0;


    }

    @Override
    public boolean hasContainsMobilePhoneNumber(String mobile) {

        UserExample userExample = new UserExample();
        userExample.createCriteria().andMobileEqualTo(mobile);
        Long userNum = userMapper.countByExample(userExample);

        int userNum1 = userNum.intValue();
        return  userNum1 != 0;
    }


}
